<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-09-17
 * Time: 16:04
 */

namespace App\Models;

/**
 * Class GoodsCategory
 * @package App\Models
 */
class GoodsCategory extends BaseModel
{
    /**
     * 与模型关联的数据表
     *
     * @var string
     */
    protected $table = 'goods_category';

    /**
     * 主键
     * @var string
     */
    protected $primaryKey = 'categoryId';

    /**
     * 需要被转换成日期的属性。
     *
     * @var array
     */
    protected $dates = ['created_at', 'updated_at'];

    /**
     * @var array
     */
    protected $guarded = ['id'];

    /**
     * @var array
     */
    protected $hidden = ['deleted_at'];

    /**
     * @var bool
     */
    public $timestamps = false;

    /**
     * @var array
     */
    protected $appends = [
        'pic_attachment',  //图标附件
    ];


    /**
     * 获取子集
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function children(){
        return $this->hasMany(self::class, 'parentId', 'categoryId');
    }

    /**
     * 商品
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function goods(){
        return $this->hasMany(Goods::class, 'categoryId', 'categoryId');
    }

    /**
     * 返回列表logo文件全路径
     * @return bool
     */
    public function getPicAttachmentAttribute()
    {
        $result = Attachment::find($this->pic);
        return $result ? $result->full_path : null;
    }

}
